package com.wei.czz.framework.admin.service.impl;

import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.utils.CopyUtils;
import com.wei.czz.common.utils.SecurityUtils;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wei.czz.framework.admin.dao.UserSequenceDao;
import com.wei.czz.framework.admin.entity.UserSequenceEntity;
import com.wei.czz.framework.admin.service.UserSequenceService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-12-11 23:18:10
 * className: UserSequenceServiceImpl
 * version: 1.0
 * description:
 */
@Service("userSequenceService")
@AllArgsConstructor
public class UserSequenceServiceImpl extends ServiceImpl<UserSequenceDao, UserSequenceEntity> implements UserSequenceService {

    private static final Logger log = LoggerFactory.getLogger(UserSequenceServiceImpl.class);

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Long get() {

        // 获取用户序号对象
        UserSequenceEntity userSequence = this.getUserSequence();
        /*

         */
        int count = baseMapper.insert(userSequence);
        log.info("保存用户序号完成。count={}", count);

        return userSequence.getId();
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Long> batchGet(Integer size) {
        List<UserSequenceEntity> userSequenceList = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            userSequenceList.add(this.getUserSequence());
        }
        boolean bool = this.saveBatch(userSequenceList);
        if (!bool) {
            log.info("批量获取用户序号失败。");
        }
        // 返回
        return CopyUtils.mapList(userSequenceList, UserSequenceEntity::getId);
    }

    @Override
    public void update(Long id, Long userId) {
        UserPo userPo = SecurityUtils.getUser();
        String username = StringUtils.EMPTY;
        Long optUserId = CommonEnum.ZERO.getLongValue();
        if (Objects.nonNull(userPo)) {
            username = userPo.getUsername();
            optUserId = userPo.getUserId();
        }
        Date date = new Date();

        UserSequenceEntity updateUserSequence = new UserSequenceEntity();
        updateUserSequence.setId(id)
                .setUseUserId(userId)
                .setUpdateTime(date)
                .setUpdateUser(username)
                .setUpdateUserId(optUserId);
        int count = baseMapper.updateById(updateUserSequence);
        log.info("更新序号所属用户完成。id={} userId={} count={}", id, userId, count);

    }

    @Transactional
    @Override
    public void batchUpdate(List<UserSequenceEntity> userSequenceList) {
        boolean bool = this.updateBatchById(userSequenceList);
        log.info("批量更新序号配置完成。bool={}", bool);
    }


    /**
     * 获取一个用户序号对象
     * @return 用户序号对象
     */
    private UserSequenceEntity getUserSequence() {
        // 获取请求用户
        UserPo userPo = SecurityUtils.getUser();
        String username = StringUtils.EMPTY;
        Long userId = CommonEnum.ZERO.getLongValue();
        if (Objects.nonNull(userPo)) {
            username = userPo.getUsername();
            userId = userPo.getUserId();
        }
        Date date = new Date();

        UserSequenceEntity userSequence = new UserSequenceEntity();
        userSequence.setUseUserId(CommonEnum.ZERO.getLongValue())
                .setDeleteStatus(CommonEnum.ZERO.getValue())
                .setUpdateTime(date)
                .setUpdateUser(StringUtils.EMPTY)
                .setUpdateUserId(CommonEnum.ZERO.getLongValue())
                .setCreateTime(date)
                .setCreateUser(username)
                .setCreateUserId(userId);
        return userSequence;
    }
}